package com.iamteer.action;

import java.util.List;

import com.iamteer.entity.Admin;
import com.iamteer.service.AdminService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	// 1. 封装请求数据
	private Admin admin;
	public Admin getAdmin() {
		return admin;
	}
	public void setAdmin(Admin admin) {
		this.admin = admin;
	}
	// 2. 调用的 Service
	private AdminService adminService = new AdminService();
	
	/**
	 * 登录
	 * @return
	 * @throws Exception
	 */
	public String login() throws Exception {
		try {
			Admin userInfo = adminService.login(admin);
			if(userInfo == null){
				return "input";
			}
			// 登录成功，保存数据到session中
			ActionContext.getContext().getSession().put("userInfo", userInfo);
			return "loginSuccess";
		} catch (Exception e) {
			return ERROR;
		}
	}
	
	/**
	 * 列表
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		try {
			List<Admin> list = adminService.getAll();
			ActionContext.getContext().getContextMap().put("listAdmin", list);
			return "list";
		} catch (Exception e) {
			return ERROR;
		}
	}
}
